const { PAGE_SIZE } = require('../config/config');
const Service = require('../service/Service');
const service = new Service();

/**
 * Web 控制器类
 * @method index() 显示首页
 * @method list() 显示列表页
 * @method tag() 显示标签列表页
 * @method new() 显示最新列表页
 * @method recommand() 显示推荐列表页
 * @method detail() 显示详情页
 * @method search() 显示搜索页
 * @method about() 显示关于页
 * @method concat() 显示联系页
 * @method release() 显示发行页
 * @method err404() 显示404错误页面
 */
class WebController {

    /**
     * 生成缓存数据
     */
    async buildCache() {
        // 生成元数据文件
        await service.buildCache();
    }

    /**
     * 显示首页
     */
    async index(req, res) {
        let pageId = parseInt(req.params.page || 1);
        res.render('index.html', await service.getIndexViewData(pageId, PAGE_SIZE));
    }

    /**
     * 显示列表页
     */
    async list(req, res) {
        let pageId = parseInt(req.params.page || 1);
        let category1 = req.params.cat1 || '', category2 = req.params.cat2 || '';
        res.render('list.html', await service.getListViewDataForCategory(pageId, PAGE_SIZE, decodeURIComponent(category1), decodeURIComponent(category2)));
    }

    /**
     * 显示标签列表页
     */
    async tag(req, res) {
        let pageId = parseInt(req.params.page || 1);
        let tag = decodeURIComponent(req.params.tag || '');
        res.render('list.html', await service.getListViewDataForTag(pageId, PAGE_SIZE, decodeURIComponent(tag)));
    }

    /**
     * 显示最新列表页
     */
    async new(req, res) {
        let pageId = parseInt(req.params.page || 1);
        res.render('list.html', await service.getListViewDataForNew(pageId, PAGE_SIZE));
    }

    /**
     * 显示推荐列表页
     */
    async recommand(req, res) {
        let pageId = parseInt(req.params.page || 1);
        res.render('list.html', await service.getListViewDataForRecommand(pageId, PAGE_SIZE));
    }

    /**
     * 显示详情页 
     */
    async detail(req, res) {
        let category1 = req.params.cat1 || '', category2 = req.params.cat2 || '', title = req.params.title || '';
        let templateFilename = 'detail.html';
        if (title.includes('layui')) {
            templateFilename = 'detail-layui.html';
        }
        if (title.includes('软件下载')) {
            templateFilename = 'detail-soft.html';
        }
        if (title.includes('源码下载')) {
            templateFilename = 'detail-soft.html';
        }
        res.render(templateFilename, await service.getDetailViewData(decodeURIComponent(title), decodeURIComponent(category1), decodeURIComponent(category2)));
    }

    /**
     * 显示搜索页
     */
    async search(req, res) {
        res.render('search.html', service.getViewData());
    }

    /**
     * 显示关于页
     */
    async about(req, res) {
        res.render('about.html', service.getViewData());
    }

    /**
     * 显示联系页
     */
    async concat(req, res) {
        res.render('concat.html', service.getViewData());
    }

    /**
     * 显示发行页
     */
    async release(req, res) {
        res.render('release.html', service.getViewData());
    }

    /**
     * 显示帮助页
     */
    async help(req, res) {
        res.render('help.html', service.getViewData());
    }

    /**
     * 显示404错误页面
     */
    async err404(req, res) {
        res.send("404");
    }
}

module.exports = WebController;